using UnityEngine;
using System.Collections;

public class UtilTeleporter : MonoBehaviour {

	public OrbitalCommand oc;
	public float speed;
	public float depth = -3;
	public float width = 250;
	public Vector3 spawnPoint;
	public bool eat = false;

	public float timer = 0;

	void FixedUpdate () {
		if (speed > 0) timer += Time.fixedDeltaTime * speed;
		if (timer > 1) {		
			foreach (Vehicle v in oc.rallyPoint.GetComponentsInChildren<Vehicle>()) {
				bool hit = v.body.position.y < depth;
				hit = hit || v.body.position.x < -width;
				hit = hit || v.body.position.x >  width;
				hit = hit || v.body.position.z < -width;
				hit = hit || v.body.position.z >  width;
				if (hit) {
					if (timer > 1) {
						if (eat) {
							//v.gameObject.SetActive(false);
							oc.remove(v.gameObject);
						} else {					
							//v.body.isKinematic = true;
							v.body.transform.position = spawnPoint;
							v.body.velocity = new Vector3(0,0,0);
							v.body.angularVelocity = new Vector3(0,0,0);
							//v.body.isKinematic = false;
							//timer--;
							timer = 0;
						}
					}
				}
			}
			if (timer > 1) timer--;
			//timer = 0;
		}

	}
}
